// pages/member/people/form/index.js
import {
  request
} from '../../../../request';
import WxValidate from "../../../../utils/WxValidate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      memberBirthday: "2022-01-01",
      memberEmail: null,
      memberId: null,
      memberName: null,
      memberPhone: null,
      memberSex: null,
    },
    sexIndex: null,
    sexString: null,
    sex: [],
  },
  onLoad: function (options) {
    //性别字典
    request("/system/dict/data/type/sys_user_sex", "GET").then(resopnse => {
      this.setData({
        sex: resopnse.data,
      });
    })
    this.initValidate();
    //修改form
    if (options.id != undefined) {
      this.loadFormData(options.id);
    }
  },
  initValidate() {
    let rules = {
      memberName: {
        required: true
      },
      memberPhone: {
        required: false,
        tel: true,
      },
      memberEmail: {
        required: false,
        email: true
      }
    }
    let message = {
      memberName: {
        required: '请输入用户昵称'
      },
      memberPhone: {
        tel: '请输入正确电话'
      },
      memberEmail: {
        required: "请输入正确邮箱"
      },
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  loadFormData(id) {
    request("/gym/member/" + id, "GET").then(res => {
      if (res.code == 200) {
        var form = res.data;
        var sex = this.data.sex.filter(item => item.dictValue == form.memberSex)[0];
        this.setData({
          sexString: sex != undefined ? sex.dictLabel : null,
          form: form
        })
        console.log(this.data);
      }
    })
  },
  sexPickerChange(e) {
    console.log(e);
    var data = this.data.form;
    data["sex"] = this.data.sex[e.detail.value].dictCode;
    this.setData({
      sexIndex: e.detail.value,
      form: data
    })
  },
  DateChange(e) {
    var formData = this.data.form;
    formData.memberBirthday = e.detail.value;
    this.setData({
      form: formData
    })
  },
  formSubmit: function (e) {
    var formData = this.data.form;
    let params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      if (!this.WxValidate.checkForm(params)) {
        let error = this.WxValidate.errorList[0];
        switch (error.param) {
          case "memberName":
            //TODO
            break;
          case "memberPhone":
            //TODO
            break;
          case "memberEmail":
            //TODO
            break;
        }
        console.log(error);
        wx.showToast({
          title: error.msg,
          icon: 'none',
          duration: 2000,
        })
        return false;
      }
    }
    var status;
    if (formData.userId != undefined) {
      status = "PUT";
    } else {
      status = "POST";
    }
    request("/gym/member", status, formData).then(res => {
      if (res.code == "200") {
        wx.showToast({
          title: '保存成功',
          icon: '',
          duration: 2000,
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  phoneInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.memberPhone = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  emailInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.memberEmail = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  nameInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.memberName = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
})